package com.cpj.aop;


import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.List;
import java.util.Set;

public class VdataUtil {

    private final static int MaxIndex=10;
    public void verification(Object o){

        Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
        if(o instanceof List){
            int j=0;
            for(int i=0;i<((List) o).size();i++){
                if(j > MaxIndex){
                    break;
                }
                Set<ConstraintViolation<Object>> constraintViolations = validator.validate(((List) o).get(i));
                for(ConstraintViolation constraintViolation:constraintViolations) {
                    System.err.println(constraintViolation.getPropertyPath().toString());
                    System.err.println(constraintViolation.getMessage());
                }
            }
        }else{
            Set<ConstraintViolation<Object>> constraintViolations = validator.validate(o);
            for(ConstraintViolation constraintViolation:constraintViolations) {
                System.err.println(constraintViolation.getPropertyPath().toString());
                System.err.println(constraintViolation.getMessage());
            }
        }
    }
}
